<template>
  <div class="order">
    <div class="order__price">
      实付金额 <b>¥{{ calculations.totalPrice }}</b>
    </div>
    <div class="order__btn" @click="handleSubmitClick(true)">提交订单</div>
  </div>
  <div class="mask" v-show="showConfirm" @click="handleSubmitClick(false)">
    <div class="mask__content" @click.stop>
      <h3 class="mask__content__title">确认要离开收银台？</h3>
      <p class="mask__content__desc">请尽快完成支付，否则将被取消</p>
      <div class="mask__content__btns">
        <div
          class="mask__content__btn mask__content__btn--first"
          @click="handleConfirmOrder(true)"
        >
          取消订单
        </div>
        <div
          class="mask__content__btn mask__content__btn--last"
          @click="handleConfirmOrder(false)"
        >
          确认支付
        </div>
      </div>
    </div>

    <div class="mask__content" v-if="confirmOrder" @click.stop>
      <h3 class="mask__content__title">支付成功，等待配送</h3>
      <div class="mask__content__icon iconfont">&#xe65a;</div>
    </div>

    <Toast v-if="show" :message="toastMessage" />
  </div>
</template>

<script>
import { ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import { useStore } from "vuex";
import { useCommonCartEffect } from "../../effects/cartEffects";
import { post } from "../../utils/requst";
import Toast, { useToastEffect } from "../../components/Toast";

// 下单相关逻辑
const useMakeOrderEffect = (shopId, shopName, productList, showToast) => {
  const router = useRouter();
  const store = useStore();

  const confirmOrder = ref(false);
  const handleConfirmOrder = async (isCanceled) => {
    const products = [];
    for (let i in productList.value) {
      const product = productList.value[i];
      products.push({ id: Number(product._id), num: product.count });
    }
    try {
      const result = await post("/api/order", {
        addressId: 1,
        shopId,
        shopName: shopName.value,
        isCanceled,
        products,
      });

      if (result?.errno === 0 && !isCanceled) {
        confirmOrder.value = true;
        store.commit("clearCartData", shopId);
        setTimeout(() => {
          router.push({ name: "OrderList" });
        }, 1000);
      } else {
        showToast("即将离开当前页面,回到首页");
        store.commit("clearCartData", shopId);
        setTimeout(() => {
          router.push({ name: "Home" });
        }, 1500);
      }
    } catch (e) {
      // 提示下单失败
      showToast("请求下单失败");
    }
  };

  return { handleConfirmOrder, confirmOrder };
};

//蒙层展示相关逻辑
const useShowMaskEffect = () => {
  const showConfirm = ref(false);

  const handleSubmitClick = (status) => {
    showConfirm.value = status;
  };

  return { showConfirm, handleSubmitClick };
};

export default {
  name: "Order",
  components: { Toast },
  setup() {
    const route = useRoute();

    const shopId = route.params.id;

    const { showConfirm, handleSubmitClick } = useShowMaskEffect();

    const { showToast, toastMessage, show } = useToastEffect();

    const { calculations, shopName, productList } = useCommonCartEffect(shopId);

    const { handleConfirmOrder, confirmOrder } = useMakeOrderEffect(
      shopId,
      shopName,
      productList,
      showToast
    );

    return {
      calculations,
      handleConfirmOrder,
      toastMessage,
      show,
      showConfirm,
      handleSubmitClick,
      confirmOrder,
    };
  },
};
</script>

<style lang="scss" scoped>
@import "../../style/viriables.scss";
.order {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  height: 0.49rem;
  line-height: 0.49rem;
  background: $bgColor;
  &__price {
    flex: 1;
    text-indent: 0.24rem;
    font-size: 0.14rem;
    color: $content-fontcolor;
  }
  &__btn {
    width: 0.98rem;
    background: #4fb0f9;
    color: #fff;
    text-align: center;
    font-size: 0.14rem;
  }
}
.mask {
  z-index: 10;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  background: rgba(0, 0, 0, 0.5);
  &__content {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 3rem;
    height: 1.56rem;
    background: #fff;
    text-align: center;
    border-radius: 0.04rem;

    &__icon {
      font-size: 0.8rem;
    }
    &__title {
      margin: 0.24rem 0 0 0;
      line-height: 0.26rem;
      font-size: 0.18rem;
      color: #333;
    }
    &__desc {
      margin: 0.08rem 0 0 0;
      font-size: 0.14rem;
      color: #666666;
    }
    &__btns {
      display: flex;
      margin: 0.24rem 0.58rem;
    }
    &__btn {
      flex: 1;
      width: 0.8rem;
      line-height: 0.32rem;
      border-radius: 0.16rem;
      font-size: 0.14rem;
      &--first {
        margin-right: 0.12rem;
        border: 0.01rem solid #4fb0f9;
        color: #4fb0f9;
      }
      &--last {
        margin-left: 0.12rem;
        background: #4fb0f9;
        color: #fff;
      }
    }
  }
}
</style>